package md.NJavaOppo;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.heytap.msp.mobad.api.ad.NativeAd;
import com.heytap.msp.mobad.api.listener.INativeAdListener;
import com.heytap.msp.mobad.api.params.INativeAdData;
import com.heytap.msp.mobad.api.params.NativeAdError;

import java.util.List;

import md.NJavaBase.INativeAdCallback;
import md.NJavaBase.INativeAdPlugin;
import md.NJavaBase.NJavaBase;

//原生广告
public class OppoNativeAd implements INativeAdPlugin  , INativeAdListener {


    //是否已经初始化完毕，可以开始加载
    public boolean isInitialized()  { return  OppoAd.isInited();  }

    //加载原生广告
    public void Load( String posId,INativeAdCallback callback) {
        Log.d(TAG,"Load");
        mCallback = callback;
        if(mLoading) return;
        mLoading = true;
        if(mNativeAd!=null) mNativeAd.destroyAd();
        mNativeAd = new NativeAd(NJavaBase.getActivity(),posId, this);
        mNativeAd.loadAd();
    }

    //原生广告显示时被调用，用于统计曝光
    public void NotifyShow() {
        NJavaBase.logOut(TAG,"NotifyShow Begin");
        mINativeAdData.onAdShow(GetView());
        NJavaBase.logOut(TAG,"NotifyShow End");
    }

    View GetView()
    {
        Activity context = NJavaBase.getActivity();
        return ((ViewGroup)context.findViewById(android.R.id.content)).getChildAt(0);
    }

    //按钮被点击，用于统计点击次数
    public void NotifyBtnClick() {
        NJavaBase.logOut(TAG,"NotifyBtnClick Begin");
        mINativeAdData.onAdClick(GetView());
        NJavaBase.logOut(TAG,"NotifyBtnClick End");
    }

    //通知关闭
    public void NotifyClose() { }

    //获取图标的Url地址，无图标返回 ""
    public String GetIconUrl() {
        return null != mINativeAdData.getIconFiles() && mINativeAdData.getIconFiles().size() > 0?mINativeAdData.getIconFiles().get(0).getUrl(): "";
    }

    //获取大图的Url地址，无图标返回 ""
    public String GetImgUrl() {
        return null != mINativeAdData.getImgFiles() && mINativeAdData.getImgFiles().size() > 0?mINativeAdData.getImgFiles().get(0).getUrl(): "";
    }


    //"广告" 标签图片，无则返回 ""
    public String GetLogUrl() {
        return null != mINativeAdData.getLogoFile()?mINativeAdData.getLogoFile().getUrl() : "";
    }

    //标题文本 ，无则返回 ""
    public String GetTitle() {
        return null != mINativeAdData.getTitle() ? mINativeAdData.getTitle() : "";
    }

    //描述文本，无则返回 ""
    public String GetDesc() {
        return null != mINativeAdData.getDesc() ? mINativeAdData.getDesc() : "";
    }

    //点击按钮文本,无则返回 ""
    public String GetClickBtnText() {
        return null != mINativeAdData.getClickBnText() ? mINativeAdData.getClickBnText() : "";
    }


    /**
     * 原生广告加载成功，在onAdSuccess回调广告数据
     *
     * @param iNativeAdDataList
     */
    @Override
    public void onAdSuccess(List<INativeAdData> iNativeAdDataList) {
        mLoading = false;
        if (null != iNativeAdDataList && iNativeAdDataList.size() > 0) {
            mINativeAdData = iNativeAdDataList.get(0);
            NJavaBase.logOut(TAG,"加载原生广告成功");
            mCallback.onLoadSuccess();
        }else
        {
            NJavaBase.logOut(TAG,"加载原生广告成功，没有附带有效信息");
            mCallback.onLoadFail();
        }
    }

    @Override
    public void onAdFailed(NativeAdError nativeAdError) {
        NJavaBase.logOut(TAG,"加载原生广告失败,错误码：" + nativeAdError.toString());
        mLoading = false;
        mCallback.onLoadFail();
    }

    @Override
    public void onAdError(NativeAdError nativeAdError, INativeAdData iNativeAdData) {
        NJavaBase.logOut(TAG, "调用原生广告统计方法出错,错误码：" + nativeAdError.toString());
    }


    private INativeAdData mINativeAdData;
    private NativeAd mNativeAd;
    private static final String TAG = "OppoAdNative";
    INativeAdCallback mCallback;
    boolean mLoading = false;

}
